<%@ page import="com.po.GoodsSingle" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%ArrayList<GoodsSingle> goods = (ArrayList<GoodsSingle>)session.getAttribute("goodslist");%>
<html>
<head>
    <title>添加物品</title>
</head>
<style type="text/css">
table{
    width: 400px;
    border:1px solid black;
}
td{
    border:1px solid black;
}
.first{
    background: lightgray;
}
</style>
<body>
    <div>
        <table id="showlist">
            <tr>
                <td colspan="2">当前有<%=goods.size()%>个商品</td>
            </tr>
            <tr class="first">
                <td>商品名称</td>
                <td>价格(元/斤)</td>
            </tr>
            <%
                Iterator<GoodsSingle> it = goods.iterator();
                while(it.hasNext()){
                    GoodsSingle g = it.next();
            %>
            <tr>
                <td><%=g.getName()%></td>
                <td><%=g.getPrice()%></td>
            </tr>
            <%
                }
            %>
        </table>
    </div>
    <br/><br/>
    <div>
        <form action="doAdd.jsp" method="get">
        <table>
            <tr>
                <td colspan="2">添加新商品</td>
            </tr>
            <tr>
                <td>商品名称</td>
                <td>商品价格</td>
            </tr>
            <tr>
                <td>
                    <input id="name" name="name" type="text"/>
                </td>
                <td>
                    <input id="price" name="price" type="text"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input onclick="return addItem()" type="submit" value="添加" />
                </td>
            </tr>
        </table>
            <input type="hidden" value="123" name="newname"/>
            <input type="hidden" value="123" name="newprice"/>
        </form>
    </div>
<script type="text/javascript">
    function addItem() {
        var list = document.getElementById("showlist").lastChild;
        var name = document.getElementById("name").value;
        var price = document.getElementById("price").value;
        if(name!=""&&price!=""){
            var tr = document.createElement("tr");
            var td = document.createElement("td");
            td.innerHTML=name;
            tr.appendChild(td);
            var td = document.createElement("td");
            td.innerHTML=price;
            tr.appendChild(td);
            list.appendChild(tr);
            return true;
        }else{
            alert("请输入商品名称和价格!");
            return false;
        }
    }
</script>
</body>
</html>
